/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.zhenhe.core.common.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.zhenhe.core.common.dao.CrudDao;
import com.zhenhe.core.common.entity.DataEntity;

/**
 * Service基类
 * @author ThinkGem
 * @version 2014-05-16
 */
@Transactional(readOnly = true)
public abstract class CrudService<D extends CrudDao<T>, T extends DataEntity> extends BaseService {
	
	/**
	 * 持久层对象
	 */
	@Autowired
	protected D dao;
	
	/**
	 * 获取单条数据
	 * @param id
	 * @return
	 */
	public T get(Integer id) {
		return dao.get(id);
	}
	
	/**
	 * 查询列表数据
	 * @param entity
	 * @return
	 */
	public List<T> findList(Map<String, Object> param) {
		return dao.findList(param);
	}
	
	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param entity
	 * @return
	 */
//	public Page<T> findPage(Page<T> page, T entity) {
//		entity.setPage(page);
//		page.setList(dao.findList(entity));
//		return page;
//	}

	/**
	 * 保存数据（插入或更新）
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public int save(T entity) {
		if (entity.getId()==null){
			return dao.insert(entity);
		}else{
			return dao.update(entity);
		}
	}
	
	/**
	 * 删除数据
	 * @param entity
	 * @return 
	 */
	@Transactional(readOnly = false)
	public Integer delete(Integer id) {
		return dao.delete(id);
	}

	@Transactional(readOnly=true)
	public T getUnique(String identity){
		return dao.getUnique(identity);
	}
	
}
